import geom.Vector;
import processing.core.PApplet;

public class Rectangle {
	float x, y, w, h;
	int id;
	static int nextID = 0;
	
	public Rectangle(Rectangle r){
		x = r.x;
		y = r.y;
		w = r.w;
		h = r.h;
		setID();
	}
	
	public Rectangle() {
		this.x = 0;
		this.y = 0;
		this.w = 0;
		this.h = 0;
		setID();
	}
	
	public Rectangle(float x, float y, float w, float h) {
		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
		setID();
	}
	
	public boolean contains(Vector s){
		return s.x >= x && s.y >= y && s.x <= x + w && s.y <= y + h;
	}
	
	public static final int NOT_CONTAINED = -1;
	public static final int CONTAINED = 0;
	public int getDimFits(Rectangle s){
		float msx = s.x + s.w;
		float msy = s.y + s.h;
		float mx = x + w;
		float my = y + h;
		int fits = 0;
		if(!(s.x >= x && s.x <= mx && s.y >= y && s.y <= my &&
			msx >= x && msx <= mx && msy >= y && msy <= my)) return NOT_CONTAINED;
		if(s.x == x && msx == mx) fits++;
		if(s.y == y && msy == my) fits++;
		return fits;
	}
	
	public boolean contains(Rectangle s){
		float msx = s.x + s.w;
		float msy = s.y + s.h;
		float mx = x + w;
		float my = y + h;
		return 	s.x >= x && s.x <= mx && s.y >= y && s.y <= my &&
				msx >= x && msx <= mx && msy >= y && msy <= my;
	}
	
	public boolean couldContain(Space s){
		return w >= s.w && h >= s.h;
		
	}
	
	
	public float getArea(){
		return w * h;
	}
	
	public boolean overlaps(Space s){
		float se = x + w;
		float sse = s.x + s.w;
		if(s.x > x && s.x < se){
			se = y + h;
			sse = s.y + s.h;
			if(s.y > y && s.y < se){
				
			}
			
		}
		if(sse > x && sse < se){
			se = y + h;
			sse = s.y + s.h;
			if(sse > y && sse < se){
				
			}
		}
		return false;
	}
	
	
	public void draw(PApplet pa){
		pa.rect(x, y, w, h);
	}
	
	public Rectangle newBoundingBox(Rectangle add){
		Rectangle old = new Rectangle(this);
		float x = (float)Math.min(old.x, add.x);
		float y = (float)Math.min(old.y, add.y);
		float x2 = (float)Math.max(add.x + add.w, old.x + old.w);
		float y2 = (float)Math.max(add.y + add.h, old.y + old.h);
		return new RectNode(x, y, x2 - x, y2 - y);
	}
	
	
	public void setID(){
		id = nextID;
		nextID++;
		if(nextID > 128000)
			nextID = 0;
	}
	public String toString(){
		StringBuffer sb = new StringBuffer();
		sb.append("Rect ID: " + id + " pos " + x + " " + y + " size " + w + " " + h);
		return sb.toString();
	}
	
	public void moveTo(float x, float y){
		this.x = x;
		this.y = y;
	}

}
